package cn.leetcode;

import java.util.Stack;

public class Code20 {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i=0; i< s.length(); i++){
            char c = s.charAt(i);
            if(stack.size() != 0){
                Character peek = stack.peek();
                if((c==')' && peek== '(') ||(c==']' && peek== '[') || (c=='}' && peek== '{')){
                    stack.pop();
                }else{
                    stack.push(c);
                }
            }else{
                stack.push(c);
            }
        }
        return stack.size() == 0;
    }

    public static void main(String[] args) {
        isValid("()[]{}");
    }
}
